<?php
/**
 * 附件
 *
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0 alpha
 * @since 2011-05-06
 * @author monkee
 */

class notepadApp extends app
{
	function __construct(){
		$this->_init();
		$this->login_need();
	}

	public function addnote(){

		$data = $this->getData('post');
		if(empty($data)){
			return array();
		}

		if($data['notecontent'] == ''){
			$data['notecontent'] = '未填写内容';
		}
		else if($data['notesubject'] == ''){
			$data['notesubject'] = strlen($data['notecontent']) > 10 ? substr($data['notecontent'], 0, 10).'...' : $data['notecontent'];
		}
		$notepadmod = $this->getModel('notepad');
		$r = $notepadmod->add($this->user['user_id'], $data['notesubject'], $data['notecontent']);

		if($r > 0){
			echo "1";
			exit;
		}else{
			echo "0";
			exit;
		}
	}

	public function index(){
		return array();
	}

	public function editnote(){

		$notepadmod = $this->getModel('notepad');

		$dataget = $this->getData('get');
		$datapost = $this->getData('post');

		if(empty($datapost)){
			$n = $notepadmod->get_notepad_by_notepad_id($dataget['id']);
			return array('n' => $n);
		}
		if($datapost['notesubject'] == ''){
			echo "2";
		}
		if($datapost['notecontent'] == ''){
			echo "2";
		}
		if($datapost['noteid'] == ''){
			echo "2";
		}
		$r = $notepadmod->modify($datapost['noteid'], $this->user_id, $datapost['notesubject'], $datapost['notecontent']);
		if($r == 1){
			echo "1";
		}
		else{
			echo "2";
		}
		exit;
	}


	public function add(){
		sleep(0.3);
		$data = $this->getData('post');
		if(empty($data)){
			return array();
		}

		if($data['notesubject'] == ''){
			$data['notesubject'] = date('Y-m-d H:i:s').'工作记事';
		}
		if($data['notecontent'] == ''){
			$data['notecontent'] = '未填写内容';
		}
		$notepadmod = $this->getModel('notepad');
		$r = $notepadmod->add($this->user['user_id'], $data['notesubject'], $data['notecontent']);
		if($r > 0){
			echo "1";
		}else{
			echo "0";
		}

	}

	public function notelist(){

		$notepadmod = $this->getModel('notepad');

		$pagesize = 100;
		$pagenum = 1;

		$notelists = $notepadmod->get_new_notepads_by_user_id($this->user_id, $pagesize, $pagenum);
		$data = $this->getData('post');
		if(empty($data)){
			return array('notepads' => $notelists);
		}
	}

	public function notedelete(){
		$data = $this->getData('get');

		if(empty($data)){
			return array('msg' => '什么也没发生');
		}
		$notepad_id = $data['notepad_id'];
		$notepad_id = intval($notepad_id);
		$notepadmod = $this->getModel('notepad');
		$r = $notepadmod->delete($notepad_id);
		if($r>0){
			return array('msg' => '删除成功', 'id' => 'notelist_tr_'.$notepad_id);
		}else{
			return array('msg' => '删除失败');
		}
		return array();
	}

	public function modify(){

		$data = $this->getData('post');
		if(empty($data)){
			return array();
		}

		if($data['subject'] == ''){
			$this->inner_msg('请输入主题');
			return array();
		}
		if($data['message'] == ''){
			$this->inner_msg('请输入内容');
			return array();
		}
		if($data['notepad_id'] == ''){
			//异常情况就是hidden的notepad_id 没有获取到
			$this->inner_msg('异常情况');
			return array();
		}

		$notepadmod = $this->getModel('notepad');
		$r = $notepadmod->modify($data['notepad_id'], $this->user['user_id'], $data['subject'], $data['message']);
		if($r > 0){
			go_to('notelist', '修改成功');
		}else{
			go_to('notelist', '修改失败');
		}

		return array();
	}

}
?>